package cn.bluethink.utile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static cn.bluethink.utile.HttpUtil.sendGet;


public class AuthenticationUtil {

    public static String token;

    public static Integer userId;

    public static String password;

    public static boolean isAuthentication() {

        if (token == null || userId == null || password == null){
            return false;
        }

        try {
            Map<String,String> param = new HashMap<>();
            param.put("userId", String.valueOf(userId));
            param.put("password",password);
            param.put("token",token);

            String res = HttpUtil.authentication("http://localhost/user/authentication",param);

            if (JsonUtil.getJsonValue(res,"message").equals("success")){
                return true;
            }else {
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @param loginUserId 登录用户id
     * @param loginPassword 登录用户密码
     * @return
     */
    public static boolean login(Integer loginUserId,String loginPassword){
        String res = null;
        try {
            res = sendGet("http://localhost/user/login","userId=" + loginUserId + "&password=" + loginPassword);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(res == null || JsonUtil.KEY_NOT_EXIT.equals(JsonUtil.getJsonValue(res,"data"))){
            return false;
        }else {
            token = JsonUtil.getJsonValue(res,"data");
            userId = loginUserId;
            password = loginPassword;
            return true;
        }
    }
}
